package com.tiantianyuegou.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SectionIndexer;
import android.widget.TextView;

import com.trouble.framework.adapter.BaseAdapterHelper;
import com.trouble.framework.adapter.QuickAdapter;
import com.tiantianyuegou.R;
import com.tiantianyuegou.model.City;
import com.tiantianyuegou.view.sticky.StickyListHeadersAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 城市列表
 * Created by huyang on 2016/1/7 0007.
 */
public class CityListAdapter extends QuickAdapter<City> implements SectionIndexer, StickyListHeadersAdapter {

    private final String[] mSectionLetters = new String[] { "定位", "A",
            "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N",
            "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
    public List<City> mHotCities;
    private HashMap<String, Integer> mSectionMap;
    private int[] mSectionIndices;
    // 定位城市
    public City mLocationcity = new City(0, "定位中", "定位");
    /**
     * 设置定位城市
     * @param city
     * @Description:
     */
    public void setLocationCity(String city) {
        if (TextUtils.isEmpty(city)) {
            mLocationcity.setCname("定位中");
        } else {
            mLocationcity.setCname(city);
        }
        notifyDataSetChanged();
    }

    public CityListAdapter(Context context) {
        super(context, R.layout.listitem_city);

        //添加定位城市
        data.add(mLocationcity);
        //添加热门城市
        String[] cities=context.getResources().getStringArray(R.array.hot_cities);
        mHotCities=new ArrayList<City>();
        for (String city:cities){
            mHotCities.add(new City(0,city,"热门"));
        }
        addAll(1, mHotCities);
    }
    private void getSectionIndices(){
        mSectionMap = new HashMap<String, Integer>();
        for (int i = 0, count = data.size(); i < count; i++) {
            City item = data.get(i);
            if (!mSectionMap.containsKey(item.getPfl())) {
                mSectionMap.put(item.getPfl(), i);
            }
        }
    }
    private Character[] getSectionLetters() {
        Character[] letters = new Character[mSectionIndices.length];
        for (int i = 0; i < mSectionIndices.length; i++) {
            letters[i] = data.get(mSectionIndices[i]).getPfl().charAt(0);
        }
        return letters;
    }

    @Override
    public void addAll(List<City> elem) {
        super.addAll(elem);
        getSectionIndices();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(layoutResId,
                    parent, false);
            holder = new ViewHolder();
            holder.text = (TextView) convertView
                    .findViewById(android.R.id.text1);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        City item = getItem(position);
        holder.text.setText(item.getCname());
        return convertView;
    }

    @Override
    protected void convert(BaseAdapterHelper helper, City item) {
        TextView text = helper.getView(android.R.id.text1);
        text.setText(item.getCname());
    }

    @Override
    public String[] getSections() {
        return mSectionLetters;
    }

    @Override
    public int getPositionForSection(int section) {
        if (mSectionMap.containsKey(mSectionLetters[section])) {
            return mSectionMap.get(mSectionLetters[section]);
        }
        return -1;
    }

    @Override
    public int getSectionForPosition(int position) {
        for (int i = 0; i < mSectionLetters.length; i++) {
            if (getItem(position).getPfl().equals(mSectionLetters[i])) {
                return i - 1;
            }
        }
        return mSectionLetters.length - 1;
    }

    @Override
    public View getHeaderView(int position, View convertView, ViewGroup parent) {
        HeaderViewHolder holder;
        if (convertView == null) {
            holder = new HeaderViewHolder();
            convertView = LayoutInflater.from(context).inflate(
                    R.layout.listitem_city_header, parent, false);
            holder.text = (TextView) convertView
                    .findViewById(android.R.id.text1);
            convertView.setTag(holder);
        } else {
            holder = (HeaderViewHolder) convertView.getTag();
        }
        City item = getItem(position);
        holder.text.setText(item.getPfl());
        return convertView;
    }

    @Override
    public long getHeaderId(int position) {
        return getItem(position).getPfl().charAt(0);
    }
    class ViewHolder {
        TextView text;
    }

    class HeaderViewHolder {
        TextView text;
    }
}
